package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Mapper
public interface OrderMapper {

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO orders (number, STATUS, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status,\n" +
            "amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time,\n" +
            "estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES\n" +
            "(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},\n" +
            "#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},\n" +
            "#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<OrderVO> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    OrderVO getVoById(Long id);

    /**
     * 根据状态统计订单数量
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select `status`,count(*) as num from `orders`  where `status` in (2,3,4) group by `status`")
    List<Map> countStatusByGroup();

    @Select("SELECT * FROM orders WHERE order_time <= #{orderTime} AND STATUS = #{status}")
    List<Orders> queryByOrderTimeAndStatus(LocalDateTime orderTime, Integer status);

    void batchUpdate(@Param("o") Orders setParams, @Param("ids") Set<Long> ids);

    Double sumAmountByParams(Map paramsMap);

    List<Map> sumAmountByparamsWithGroupby(Map paramsMap);

    Integer countUserByParams(Map paramsMap);

    List<Map> countUserByParamsWithGroupby(Map paramsMap);

    List<Map> orderCountByParamsWithGroupby(Map paramsMap);

    List<GoodsSalesDTO> salesTop10ByParams(Map paramsMap);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
